package com.zllkj.mm.module.home.fragments

import androidx.appcompat.widget.AppCompatImageView
import androidx.appcompat.widget.AppCompatTextView
import androidx.recyclerview.widget.LinearLayoutManager
import com.zllkj.mm.R
import com.zllkj.mm.adapter.BlockListAdapter
import com.zllkj.mm.adapter.CollectionListAdapter
import com.zllkj.mm.api.network.RetrofitHelper
import com.zllkj.mm.api.network.RxHelper
import com.zllkj.mm.api.network.subscriber.ProgressSubscriber
import com.zllkj.mm.base.BaseTitleFragment
import com.zllkj.mm.common.L
import com.zllkj.mm.model.entity.CollectionsListBean
import com.zllkj.mm.model.entity.DynamicBlockItemVo
import com.zllkj.mm.model.entity.DynamicBlockItemVoRecord
import com.zllkj.mm.module.goods.GoodsDetailFragment
import com.zllkj.mm.module.goods.vm.ItemCollectionsVM
import com.zllkj.mm.module.shopping.fragment.OrderListItemFragment
import com.zllkj.mm.util.common.FormatUtil
import kotlinx.android.synthetic.main.fragment_mycollection.view.*
import java.util.HashMap

class BlockListFragment:BaseTitleFragment() {

    var datas = ArrayList<DynamicBlockItemVoRecord>()
    lateinit var adapter : BlockListAdapter
    var currentPage = 1


    override fun getContentLayoutId(): Int {
        return R.layout.fragment_mycollection
    }


    fun removeBlock(position:Int){
        addSubscribe(RetrofitHelper.getInstance().dynamicApi.releaseDynamicBlockUser((HashMap<String, Any>().apply {
            this["userId"] = datas[position].userId
        })).compose<String>(RxHelper.defaultHandleResult<String>()).subscribe(object : ProgressSubscriber<String>(requireContext()){
            override fun onSucc(t: String?) {
                L.showToast("移除成功")
                datas.removeAt(position)
                adapter.notifyDataSetChanged()
            }

            override fun onError(e: Throwable?) {
                super.onError(e)
            }

        }))
    }


    override fun loadData() {
        super.loadData()
        setCommonTitle("屏蔽用户")
        adapter = BlockListAdapter(R.layout.item_block_user_list,datas)
        var emptyView = layoutInflater.inflate(R.layout.include_empty_layout,null).apply {
            findViewById<AppCompatImageView>(R.id.iv_empty_icon).setImageResource(R.mipmap.ic_empty_friend)
            findViewById<AppCompatTextView>(R.id.tv_empty_title).text = "这里空空如也"
            findViewById<AppCompatTextView>(R.id.tv_empty_content).text = "“您没有屏蔽的黑名单哦~”"
        }
        adapter.emptyView = emptyView
        mRootView.rv_mycollection.layoutManager = LinearLayoutManager(requireContext())
        mRootView.rv_mycollection.adapter = adapter


        adapter.setOnItemChildClickListener { baseQuickAdapter, view, i ->
            if (view.id == R.id.btn_remove_blocker){
                removeBlock(i)
            }

        }


        mRootView?.smartRefreshlayout_mycollection?.setOnRefreshListener {
            datas.clear()
            currentPage = 1
            loadNetWorkData(OrderListItemFragment.PULL_DOWN)
        }

        mRootView?.smartRefreshlayout_mycollection?.setOnLoadMoreListener {
            currentPage += 1
            loadNetWorkData(OrderListItemFragment.LOAD_MORE)
        }
        loadNetWorkData(-1)
    }

    fun loadNetWorkData(loadStatus:Int){
        addSubscribe(RetrofitHelper.getInstance().dynamicApi.getDynamicBlockList((HashMap<String, Any>().apply {
            this["goodsName"] = ""
            this["currentPage"] = currentPage
            this["pageSize"] = 10
        })).compose<DynamicBlockItemVo>(RxHelper.defaultHandleResult<DynamicBlockItemVo>()).subscribe(object : ProgressSubscriber<DynamicBlockItemVo>(requireContext()){
            override fun onSucc(t: DynamicBlockItemVo?) {
                t?.records?.let {
                    it.map { data->
                        datas.add(data)
                    }
                }

                if(loadStatus == OrderListItemFragment.PULL_DOWN) {
                    mRootView.smartRefreshlayout_mycollection.finishRefresh()
                    adapter.notifyDataSetChanged()
                } else if (loadStatus == OrderListItemFragment.LOAD_MORE) {
                    mRootView.smartRefreshlayout_mycollection.finishLoadMore()
                    adapter.notifyDataSetChanged()
                } else {
                    adapter.notifyDataSetChanged()
                }
            }

            override fun onError(e: Throwable?) {
                super.onError(e)
            }

        }))

    }

    companion object {
        @JvmStatic
        fun newInstance(): MyCollectionFragment {
            return MyCollectionFragment()
        }
    }
}